<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      th:replace="layout/base :: layout(~{::title}, ~{::main})">
<head>
    <title>登录 - 博客系统</title>
</head>
<body>
    <main>
        <div class="container mt-5">
            <div class="row justify-content-center">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="text-center">登录</h3>
                        </div>
                        <div class="card-body">
                            <div th:if="${param.error}" class="alert alert-danger">
                                用户名或密码错误
                            </div>
                            <div th:if="${param.logout}" class="alert alert-success">
                                已成功退出
                            </div>
                            <form id="loginForm" onsubmit="handleLogin(event)">
                                <div class="mb-3">
                                    <label for="username" class="form-label">用户名</label>
                                    <input type="text" class="form-control" id="username" name="username" required>
                                </div>
                                <div class="mb-3">
                                    <label for="password" class="form-label">密码</label>
                                    <input type="password" class="form-control" id="password" name="password" required>
                                </div>
                                <button type="submit" class="btn btn-primary w-100">登录</button>
                            </form>
                            <div class="mt-3 text-center">
                                <a href="/register">还没有账号？立即注册</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script src="/js/auth.js"></script>
        <script>
            function handleLogin(event) {
                event.preventDefault();
                
                const username = document.getElementById('username').value;
                const password = document.getElementById('password').value;

                fetch('/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                })
                .then(response => response.json())
                .then(handleLoginResponse)
                .catch(error => {
                    console.error('Error:', error);
                    alert('登录失败，请重试');
                });
            }
        </script>
    </main>
</body>
</html> 